package com.sniff.atp.modules.test.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sniff.atp.modules.test.mapper.BaseDataMapper;
import com.sniff.atp.modules.test.model.BaseData;
import com.sniff.atp.modules.test.service.BaseDataService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-03-21
 */
@Service
public class BaseDataServiceImpl extends ServiceImpl<BaseDataMapper, BaseData> implements BaseDataService {

    @Autowired
    private BaseDataMapper baseDataMapper;

    @Override
    public boolean create(BaseData baseData) {
        baseData.setCreateTime(new Date());
        baseData.setUpdateTime(new Date());

        return save(baseData);
    }

    @Override
    public boolean update(Long id, BaseData baseData) {
        baseData.setId(id);
        baseData.setUpdateTime(new Date());
        return updateById(baseData);
    }

    @Override
    public Page<BaseData> pageList(BaseData baseData, Integer pageSize, Integer pageNum) {
        Page<BaseData> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(StringUtil.isNotBlank(baseData.getDataCode()),BaseData::getDataCode,baseData.getDataCode())
                .eq(StringUtil.isNotBlank(baseData.getDataName()),BaseData::getDataName,baseData.getDataName())
                .eq(StringUtil.isNotBlank(baseData.getDataType()),BaseData::getDataType,baseData.getDataType())
                .orderByDesc(BaseData::getDataCode)
                .orderByDesc(BaseData::getSort);
        return page(page, wrapper);
    }

    @Override
    public List<BaseData> list(BaseData baseData) {
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(StringUtil.isNotBlank(baseData.getDataCode()),BaseData::getDataCode,baseData.getDataCode())
                .eq(StringUtil.isNotBlank(baseData.getDataName()),BaseData::getDataName,baseData.getDataName())
                .eq(StringUtil.isNotBlank(baseData.getDataType()),BaseData::getDataType,baseData.getDataType())
                .orderByDesc(BaseData::getDataCode)
                .orderByAsc(BaseData::getSort);
        return baseDataMapper.selectList(wrapper);
    }

    @Override
    public List<String> singlelist(BaseData baseData) {
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(StringUtil.isNotBlank(baseData.getDataCode()),BaseData::getDataCode,baseData.getDataCode())
                .eq(StringUtil.isNotBlank(baseData.getDataName()),BaseData::getDataName,baseData.getDataName())
                .eq(StringUtil.isNotBlank(baseData.getDataType()),BaseData::getDataType,baseData.getDataType())
                .orderByDesc(BaseData::getDataCode)
                .orderByAsc(BaseData::getSort);
        List<BaseData> baseDataList = baseDataMapper.selectList(wrapper);
        return baseDataList.stream().map(a -> String.valueOf(a.getDataName())).collect(Collectors.toList());
    }
}
